package com.gu.controller;

import com.gu.entity.EduChapter;
import com.gu.entity.vo.ChapterVo;
import com.gu.service.EduChapterService;
import entity.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import javax.annotation.Resource;

/**
 * Description: 章节 控制组件
 *
 * @Autor: Rolin
 * datetime: 05-21-15:46
 */
@Api(tags = "课程章节管理")
@RequestMapping("/eduService/chapter")
@RestController
@CrossOrigin
public class EduChapterController {

    @Resource
    private EduChapterService eduChapterService;

    @ApiOperation(value = "根据courseId查询所有章节下的小节")
    @GetMapping("/getChaptersAndVideos/{id}")
    public CommonResult getChaptersAndVideos(@PathVariable(value = "id") String courseId) {
        List<ChapterVo> list = eduChapterService.getChapters(courseId);
        return list.size() > 0 ? CommonResult.success().data("list", list) : CommonResult.error();
    }

    @ApiOperation("根据chapterId查询章节信息")
    @GetMapping("getChapterById/{id}")
    public CommonResult getChapterById(@PathVariable("id")String chapterId){
        EduChapter chapter = eduChapterService.getById(chapterId);
        return !StringUtils.isEmpty(chapter.getTitle()) ? CommonResult.success().data("chapter",chapter) : CommonResult.error();
    }

    @ApiOperation("删除章节")
    @DeleteMapping("/deleteChapter/{chapterId}")
    public CommonResult deleteChapterByChapterId(@PathVariable("chapterId")String chapterId){
            boolean result  = eduChapterService.deleteChapterByChapterId(chapterId);
            return result ? CommonResult.success().data("result",true): CommonResult.error().data("result",false);
    }

    @ApiOperation("添加章节")
    @PostMapping("/saveChapter")
    public CommonResult saveChapter(@RequestBody ChapterVo chapterVo){
       String chapterId =  eduChapterService.saveChapter(chapterVo);
       return chapterId!=null ? CommonResult.success().data("chapterId",chapterId): CommonResult.error();
    }

    @ApiOperation("更新章节")
    @PutMapping("/updateChapter/{chapterId}")
    public CommonResult updateById(
            @PathVariable("chapterId")String chapterId,
            @RequestBody ChapterVo chapterVo){

       boolean result  = eduChapterService.updateById(chapterId,chapterVo);
        return result ? CommonResult.success().data("result",true): CommonResult.error().data("result",false);
    }


}
